package cn.com.idmy.auth.oauth;

import cn.com.idmy.auth.Authenticator;
import cn.com.idmy.auth.context.Request;
import cn.com.idmy.auth.context.Response;
import cn.com.idmy.auth.login.LoginManager;
import cn.com.idmy.base.util.Assert;
import lombok.Data;
import lombok.experimental.Accessors;
import org.jetbrains.annotations.NotNull;

import static cn.com.idmy.auth.oauth.OauthConsts.GRANT_TYPE;

@Data
@Accessors(fluent = true)
public class OauthManager<ID> {
    protected LoginManager<ID> loginManager;
    protected Authenticator<Request, ?, ID> authenticator;
    protected OauthClientDao oauthClientDao;
    protected AccessTokenFactory<ID> accessTokenFactory;

    @NotNull
    public AccessToken getToken(@NotNull Request request, @NotNull Response response) {
        var grantType = GrantType.of(Assert.notBlank(request.getParam(GRANT_TYPE), "grant_type 不能为空"));
        return accessTokenFactory.get(grantType).getToken(request, response);
    }
}
